//
//  TSNoticeTableViewCell.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/6.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import HandyJSON

struct TSNoticeModel : HandyJSON {
    
    var addtime : Int64 = 0
    var content : String? = nil
    var id = -1
    var title : String? = nil
    var titlename : String? = nil
}

class TSNoticeTableViewCell: UITableViewCell {
    
    var model : TSNoticeModel?
    
//    var dateLabel : UILabel!
    var typeLabel : UILabel!
    var titleLabel : UILabel!
    var contentLabel : UILabel!
    var publishLabel : UILabel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func setupCell(model : TSNoticeModel) {
        self.model = model
        
        typeLabel.text = model.title
        titleLabel.text = model.titlename
        contentLabel.text = model.content
        let timeStr = Date.timeStampToString(timeStamp: model.addtime, outputFormatter: "yyyy-MM-dd HH:mm:ss")
        publishLabel.text = "发布时间："+timeStr
    }
    
    func configViews() {
        
        let bgView = UIImageView.init(image: UIImage(named: "main_wallet_bg"))
        self.contentView.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(10)
            make.width.equalTo(SCREEN_WIDTH - 2*12)
            
            make.bottom.equalTo(0.001)
        }
        
        typeLabel = createLabel(text: "", textColor: "#282828", fontSize: 16)
        typeLabel.textAlignment = .center
        bgView.addSubview(typeLabel)
        typeLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(10)
            make.left.right.equalToSuperview().inset(10)
        }
        
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        bgView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(typeLabel.snp_bottom).offset(10)
            make.height.equalTo(1)
        }
        
        titleLabel = createLabel(text: "", textColor: "#282828", fontSize: 14)
        titleLabel.numberOfLines = 0
        bgView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(10)
            make.top.equalTo(line.snp_bottom).offset(5)
        }
        
        contentLabel = createLabel(text: "", textColor: "#282828", fontSize: 14)
        contentLabel.numberOfLines = 0
        bgView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { (make) in
            make.left.right.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp_bottom).offset(20)
        }
        
        publishLabel = createLabel(text: "", textColor: "#989898", fontSize: 14)
        bgView.addSubview(publishLabel)
        publishLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(10)
            make.top.equalTo(contentLabel.snp_bottom).offset(28)
            
            make.bottom.equalTo(-15)
        }
        
    }

    func createLabel(text:String, textColor:String, fontSize:CGFloat) -> UILabel {
        
        let label = UILabel.init()
        label.text = text
        label.textColor = UIColor.hexadecimalColor(hexadecimal: textColor)
        label.font = UIFont.systemFont(ofSize: fontSize, weight: .medium)
        return label
    }
}
